import React, { Component } from 'react'
import "./index.css"

export default class Item extends Component {
    state = {mouse:false}
    handleMouse = (flag) => {
        return () => {
            this.setState({mouse:flag})
        }
    }
    handleCheck = (id) => {
        return (event) => {
            // console.log(id,event.target.checked)
            this.props.updateTodo(id,event.target.checked)
        }
    }
    // 删除
    removeItem = (id) => {
        if(window.confirm("确定删除吗？")){
            this.props.removeItem(id)
        }
    }
    render() {
        const {id,title,done} = this.props;
        const {mouse} = this.state
        return (
            <li
             style={{backgroundColor: mouse ? '#ddd' : 'white'}}
             onMouseEnter={this.handleMouse(true)}
             onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleCheck(id)} />
                    <span>{title}</span>
                </label>
                <button onClick={() => this.removeItem(id)} className="btn btn-danger" style={{ display: mouse ? 'block' : "none" }}>删除</button>
            </li>
        )
    }
}
